package com.henu.engineer.service;

import com.github.pagehelper.PageHelper;
import com.henu.engineer.dao.entity.student.Student;
import com.henu.engineer.dao.entity.User;
import com.henu.engineer.dao.mapper.student.StudentXmlMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author noexcept
 * @date 2021/7/22 22:14
 */
@Service
public class StudentService{

    @Autowired
    private StudentXmlMapper studentXmlMapper;

    /**
     * 根据学号查找学生
     * @param id
     * @return
     */
    public Student selectStudent(String id) {
        return studentXmlMapper.selectStudent(id);
    }

    /**
     * 根据多个学号查找多个学生
     * @param studentIds
     * @return
     */
    public List<Student> selectStudentsById(List<String> studentIds){
        return studentXmlMapper.selectStudentsById(studentIds);
    }

    /**
     * 根据多个学号查找多个学生
     * @param studentIds
     * @param pageNumber 第几页， 从1开始
     * @param pageSize 一页多少行数据
     */
    public List<Student> selectStudentsById(List<String> studentIds,Integer pageNumber,Integer pageSize){
        PageHelper.startPage(pageNumber,pageSize);
        List<Student> students = studentXmlMapper.selectStudentsById(studentIds);
        return students;
    }

    /**
     * 根据专业查询多个学生
     */
    public List<Student> selectStudentsBySpecialization(String specialization){
       return studentXmlMapper.selectStudentsBySpecialization(specialization);
    }

    /**
     * 根据专业查询多个学生
     */
    public List<Student> selectStudentsBySpecialization(String specialization, Integer pageNumber, Integer pageSize){
        PageHelper.startPage(pageNumber,pageSize);
        List<Student> students = studentXmlMapper.selectStudentsBySpecialization(specialization);
        return students;
    }

    /**
     * 根据专年级查询多个学生
     */
    public List<Student> selectStudentsByGrade(String grade){
        return studentXmlMapper.selectStudentsByGrade(grade);
    }

    /**
     * 根据年级查询多个学生
     */
    public List<Student> selectStudentsByGrade(String grade,Integer pageNumber,Integer pageSize){
        PageHelper.startPage(pageNumber,pageSize);
        List<Student> students = studentXmlMapper.selectStudentsByGrade(grade);
        return students;
    }

    /**
     * 根据专业和年查询多个学生
     */
    public List<Student> selectStudentsBySpecAndGrade(String specialization,String grade){
        List<Student> students = studentXmlMapper.selectStudentsBySpecAndGrade(specialization, grade);
        return students;
    }

    /**
     * 根据专业和年级查询多个学生
     */
    public List<Student> selectStudentsBySpecAndGrade(String specialization,String grade,Integer pageNumber,Integer pageSize){
        PageHelper.startPage(pageNumber,pageSize);
        List<Student> students = studentXmlMapper.selectStudentsBySpecAndGrade(specialization, grade);
        return students;
    }
    /**
     * 新增学生
     * @param student
     * @param pwd
     * @return
     */
    public boolean insertStudent(Student student,String pwd) {
        boolean b = false;
        try {
            b = studentXmlMapper.insertStudent(student);
            studentXmlMapper.insertUser(new User(student.getId(),pwd,UserService.STUDENT));
        } catch (DuplicateKeyException e) {
            UserService.logger.error("学生表插入了主键相同的记录！");
        }

        return b;
    }

    /**
     * 更新学生联系方式
     * @param id
     * @param contact
     * @return 是否成功
     */
    public boolean updateContact(String id, String contact) {
        return studentXmlMapper.updateContact(id, contact);
    }

    /**
     * 更新学生专业
     * @param id
     * @param specialization
     * @return 是否成功
     */
    public boolean updateSpecialization(String id, String specialization) {
        return studentXmlMapper.updateSpecialization(id, specialization);
    }



}
